package com.huike.stock_analysis.service;

import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.stock_analysis.dao.FinanceDao;
import com.huike.stock_analysis.entity.GrowInfo;


/**
 * 
 * @author mzLEI
 * @date 2020年7月2日
 */
/*
 * 成长能力service
 */
@Service
public class GrowService {
	//注入dao
		@Autowired
		private FinanceDao financeDao;
		
		//保存数据库查询到的数据
		private List<GrowInfo> growInfos;
		
		//保留四位小数
		private DecimalFormat df = new DecimalFormat("0.0000");
		
		private void setList(String code) {
		
				growInfos = financeDao.queryGrow(code);
			
		}
		
		/**
		 * 
		 * @param code
		 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
		 * 总资产增长率
		 */
		public Map<String, String> assetsGrowRatio(String code) {
			//创建返回map
			Map<String, String> map = new LinkedHashMap<>();
			setList(code);
			
			
			//遍历查询结果 找到营业利润率
			for(GrowInfo growInfo : growInfos) {
				//System.out.print(profitInfo.getDate());
				map.put(growInfo.getDate().toString(), df.format(growInfo.getTotalAssetsGrowRatio()) + "," + df.format(growInfo.getTotalAssetsGrowRatioOfSame()));
			}
			
			return map;
		}
		
		
		/**
		 * 
		 * @param code
		 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
		 * 营业收入增长率
		 */
		public Map<String, String> businessIncomeGrowRatio(String code) {
			//创建返回map
			Map<String, String> map = new LinkedHashMap<>();
			
			//查询数据库
			setList(code);
			
			//遍历查询结果 
			for(GrowInfo growInfo : growInfos) {
				//System.out.print(profitInfo.getDate());
				map.put(growInfo.getDate().toString(), df.format(growInfo.getMainBusinessGrowRatio()) + "," + df.format(growInfo.getAvrMainBusinessGrowRatioOfSame()));
			}
			return map;
		}
		
		/**
		 * 
		 * @param code
		 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
		 *  净利润增长率
		 */
		public Map<String, String> netProfitGrowRatio(String code) {
			//创建返回map
			Map<String, String> map = new LinkedHashMap<>();
			
			setList(code);
			
			
			//遍历查询结果
			for(GrowInfo growInfo : growInfos) {
				//System.out.print(profitInfo.getDate());
				map.put(growInfo.getDate().toString(), df.format(growInfo.getNetProfitGrowRatio()) + "," + df.format(growInfo.getNetAssetsGrowRatio()));
			}
			return map;
		}
		
		
		/**
		 * 
		 * @param code
		 * @return Map<String, String> 返回的数据 k为日期 v为对应的数据
		 *  净资产增长率
		 */
		/*
		public Map<String, String> netAssetsGrowRatio() {
			//创建返回map
			Map<String, String> map = new LinkedHashMap<>();
			
			//查询数据库
			
			
			//遍历查询结果 
			for(GrowInfo growInfo : growInfos) {
				//System.out.print(profitInfo.getDate());
				map.put(growInfo.getDate().toString(), df.format(growInfo.getNetAssetsGrowRatio()));
			}
			return map;
		}
		*/
		
		/**
		 * 
		 * @param code
		 * @return String 最近的一个数据
		 *  固定资产比重
		 */
		public String fixesAssetsRatio(String code) {
			//获取最近的一个数据
			setList(code);
			GrowInfo growInfo = growInfos.get(growInfos.size() - 1);
			return df.format(growInfo.getFixesAssetsRatio());
		}
		
		
		

}
